package com.cosyhut.blocks.jode;

import java.util.ArrayList;
import com.cosyhut.blocks.engine.*;

import com.jme.math.*;

/**
 * 
 * @author TimWorks
 * Feb 05, 2007
 *
 */
public class ODEBlock {
	protected ODEWorld _world;
    protected SWIGTYPE_p_dxBody _body;

    protected SWIGTYPE_p_dxSpace _space;
    private ArrayList<ODEGeom> _geoms;
    
    protected SWIGTYPE_p_dxJointGroup _group;
    private ArrayList<ODEJoint> _joints;
    private ArrayList<ODEJointPoint> _jointpoints;
    

    public ODEBlock(Engine engine) {
    	this._world = engine.getWorld();
        this._body = jode.dBodyCreate(this._world.getWorld());
        this._space = jode.dHashSpaceCreate(engine.getSpace());
        this._geoms = new ArrayList<ODEGeom>();
        this._group = jode.dJointGroupCreate(0);
        this._joints = new ArrayList<ODEJoint>();
        this._jointpoints = new ArrayList<ODEJointPoint>();
    }
    
    public Vector3f getPosition() {
        SWIGTYPE_p_float p = jode.new_floatArray(3);
        p = jode.dBodyGetPosition(this._body);
        return ODEHelper.fromArraytoVector3f(p);
    }

    public void setPosition(Vector3f position) {
        jode.dBodySetPosition(this._body, position.x, position.y, position.z);
    }

    public Matrix3f getRotation() {
        SWIGTYPE_p_float r = jode.new_floatArray(12);
        r = jode.dBodyGetRotation(this._body);
        return ODEHelper.fromArraytoMatrix3f(r);
    }

    public void setRotation(Matrix3f rotation) {
        jode.dBodySetRotation(this._body, ODEHelper.fromMatrix3ftoArray(rotation));
    }

    public void addGeom(ODEGeom geom, Vector3f position, Matrix3f rotation) {
        this._geoms.add(geom);
        geom.attach(this, position, rotation);
    }
    public void removeGeom(ODEGeom geom) {
        this._geoms.remove(geom);
    }
    public void addJoint(ODEJoint joint, Vector3f position, Matrix3f rotation) {
        this._joints.add(joint);
        joint.attach(this, position, rotation);
    }
    public void removeJoint(ODEJoint joint) {
        this._joints.remove(joint);
    }
    public void addJointPoint(ODEJointPoint jp, Vector3f position, Matrix3f rotation) {
        this._jointpoints.add(jp);
        jp.attach(this, position, rotation);
    }
    public void removeJointPoint(ODEJoint jp) {
        this._joints.remove(jp);
    }

    //--------------------------------------------------------------------------
    public SWIGTYPE_p_dxWorld getWorld() {
    	return this._world.getWorld();
    }
    public SWIGTYPE_p_dxBody getBody() {
        return this._body;
    }
    public ArrayList<ODEGeom> getGeoms() {
        return this._geoms;
    }
    public ArrayList<ODEJoint> getJoints() {
        return this._joints;
    }
    public ArrayList<ODEJointPoint> getJointPoints() {
        return this._jointpoints;
    }
    public SWIGTYPE_p_dxSpace getSpace() {
        return this._space;
    }
    public Vector3f toWorldVector(Vector3f v) {
        SWIGTYPE_p_float result = jode.new_floatArray(3);
    	jode.dBodyVectorToWorld(this._body, v.x, v.y, v.z, result);
    	return ODEHelper.fromArraytoVector3f(result);
    }
    public SWIGTYPE_p_dxJointGroup getGroup() {
    	return this._group;
    }
}
